package com.softaria.gwt.common.client.widget;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Info {

  private static final int titleHeight = 20;
  private static final int messHeight = 80;
  // 14 is sum of all padding and margin
  private static final int realInfoHeight = titleHeight + messHeight + 14;

  private static final int messWidth = 250;
  // 14 is sum of all padding and margin
  private static final int realInfoWidth = messWidth + 14;

  // this constant use as space between info and browser border
  private static final int space = 5;

  private static final int showTime = 2500;

  public static void show(String title, String HtmlMessage) {
    int totalWidth = Window.getClientWidth();
    int totalHeight = Window.getClientHeight();

    final PopupPanel popup = new PopupPanel(false);
    popup.setStylePrimaryName("sa-popup");
    popup.setAnimationEnabled(true);

    HorizontalPanel titlePanel = new HorizontalPanel();
    Label titleLabel = new Label(title);
    titleLabel.setStylePrimaryName("infoTitle");
    titlePanel.add(titleLabel);
    titlePanel.setSize(messWidth + "px", titleHeight + "px");

    HorizontalPanel messagePanel = new HorizontalPanel();
    HTML messageLabel = new HTML(HtmlMessage);
    messageLabel.setStylePrimaryName("infoText");
    messagePanel.add(messageLabel);
    messagePanel.setSize(messWidth + "px", messHeight + "px");

    int top = totalHeight - realInfoHeight - space;
    int left = totalWidth - realInfoWidth - space;

    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.setStylePrimaryName("titleAndTextContainer");
    verticalPanel.add(titlePanel);
    verticalPanel.add(messagePanel);

    DecoratorPanel decoratorPanel = new DecoratorPanel();
    decoratorPanel.setStylePrimaryName("sa-roundRectangleFilledPanel");
    decoratorPanel.add(verticalPanel);
    popup.add(decoratorPanel);
    popup.setPopupPosition(left, top);
    popup.show();

    Timer t = new Timer() {
      public void run() {
        popup.hide();
      }
    };
    t.schedule(showTime);
  }
}
